﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace InhollandAir.App_Code
{
    public class Flight
    {
        private int mId;
        private string mDepartureDate;
        private string mDepartureTime;
        private string mStatus;
        private Aircraft mAircraft;
        private Airport mAirportFrom;
        private Airport mAirportTo;
        private string mPrice;
        private string mTravelTime;
        private string mFlightNr;

        public Flight()
        {
            mAircraft = new Aircraft();
            mAirportFrom = new Airport();
            mAirportTo = new Airport();
        }

        public Flight(int reservationId)
        {
            //get flightId from reservation
            var connUser = new SqlConnection(WebConfigurationManager.ConnectionStrings["FlightReservation"].ToString());

            if (connUser.State == ConnectionState.Closed) connUser.Open();

            int flightId = 0;
            try
            {
                string sqlText = "SELECT flightID FROM reservation WHERE reservationID = @reservationId";
                SqlCommand cmd = new SqlCommand(sqlText, connUser);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@reservationId", reservationId);
                SqlDataReader myReader = cmd.ExecuteReader();

                if (myReader.Read())
                {
                    flightId = (int)myReader["flightID"];
                }
                else
                {
                    throw new Exception("No flight found with flightID= " + flightId);
                }
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connUser.Close();
            }
            //load flight from db
            mAircraft = new Aircraft();
            mAirportFrom = new Airport();
            mAirportTo = new Airport();
            try
            {
                LoadFlightFromDb(flightId);
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void LoadFlightFromDb(int flightId)
        {
            //get flight
            var connUser = new SqlConnection(WebConfigurationManager.ConnectionStrings["FlightReservation"].ToString());

            if (connUser.State == ConnectionState.Closed) connUser.Open();

            int aircraftId = 0;
            int airportIdFrom = 0;
            int airportIdTo = 0;

            try
            {
                string sqlText = "SELECT * FROM flight WHERE flightID = @flightId";
                SqlCommand cmd = new SqlCommand(sqlText, connUser);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@flightId", flightId);
                SqlDataReader myReader = cmd.ExecuteReader();

                if (myReader.Read())
                {
                    this.mId = (int)myReader["flightID"];
                    this.mDepartureDate = myReader["departureDate"].ToString();
                    this.mDepartureTime = myReader["departureTime"].ToString();
                    this.mStatus = myReader["status"].ToString();
                    this.mPrice = myReader["airmiles"].ToString();
                    this.mTravelTime = myReader["TravelTime"].ToString();
                    this.mFlightNr = myReader["IntFlightNum"].ToString();

                    //get id from airports and aircraft
                    aircraftId = (int)myReader["aircraftID"];
                    airportIdFrom = (int)myReader["airportID_from"];
                    airportIdTo = (int)myReader["airportID_to"];
                }
                else
                {
                    throw new Exception("No flight found with flightID= " + id);
                }

                this.mAircraft.LoadFromDb(aircraftId);
                this.mAirportFrom.LoadFromDb(airportIdFrom);
                this.mAirportTo.LoadFromDb(airportIdTo);
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connUser.Close();
            }            
        }

        #region Properties

        public int id
        {
            get { return mId; }
            set { mId = value; }
        }

        public string departureDate
        {
            get { return mDepartureDate; }
            set { mDepartureDate = value; }
        }

        public string departureTime
        {
            get { return mDepartureTime; }
            set { mDepartureTime = value; }
        }

        public string status
        {
            get { return mStatus; }
            set { mStatus = value; }
        }

        public Aircraft aircraft
        {
            get { return mAircraft; }
            set { mAircraft = value; }
        }

        public Airport airportFrom
        {
            get { return mAirportFrom; }
            set { mAirportFrom = value; }
        }

        public Airport airportTo
        {
            get { return mAirportTo; }
            set { mAirportTo = value; }
        }

        public string price
        {
            get { return mPrice; }
            set { mPrice = value; }
        }

        public string travelTime
        {
            get { return mTravelTime; }
            set { mTravelTime = value; }
        }

        public string flightNr
        {
            get { return mFlightNr; }
            set { mFlightNr = value; }
        }

        #endregion
    }
}